﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Threading;

namespace AtlasGis
{
  
    
    public partial class ucThiTN : UserControl
    {
        private ketnoi kn = new ketnoi();
        private string sql;
        private string[] strChude, strCH, strA, strB, strC, strD, strDA, strChon;
        private int slCH = 0 , pos = 0;
        public ucThiTN()
        {            
            InitializeComponent();
            

            sql = "select * from dbo.chude";
            DataTable dt = kn.LayDuLieu(sql);

            int rows = dt.Rows.Count;

            if ( rows > 0)
            {               
                cboChude.Items.Clear();
                strChude = new string[50];

                for (int i = 0; i < rows; i++ )
                {
                    cboChude.Items.Add(dt.Rows[i][1].ToString());                    
                    strChude[i] = string.Copy(dt.Rows[i][0].ToString());
                 }

                cboChude.SelectedIndex = 0;
            }
            
        }

        private void txtSoCH_TextChanged(object sender, TextChangedEventArgs e)
        {
            if ((txtSoCH.Text.Length > 0) && ("0123456789".IndexOf(txtSoCH.Text[txtSoCH.Text.Length - 1]) == -1))
                txtSoCH.Text = txtSoCH.Text.Substring(0, txtSoCH.Text.Length - 1);
        }

        
        private int get_slCH(int slNhap, string chude)
        {
            sql = "select CH_ma from dbo.cauhoi where cd_ma = '" + chude + "'";
           // MessageBox.Show(sql);
            DataTable dt = kn.LayDuLieu(sql);
            return (dt.Rows.Count<slNhap ? dt.Rows.Count:slNhap);            
        }

        private void reset_RDA()
        {
            rdbA.IsChecked = false;
            rdbB.IsChecked = false;
            rdbC.IsChecked = false;
            rdbD.IsChecked = false;
            rdbA.FontWeight = FontWeights.Normal;
            rdbB.FontWeight = FontWeights.Normal;
            rdbC.FontWeight = FontWeights.Normal;
            rdbD.FontWeight = FontWeights.Normal;
            this.UpdateLayout();
        }

        private void load_CH(int vt)
        {                      
            if (vt == 0) 
                btnTruoc.IsEnabled = false;
            else 
                btnTruoc.IsEnabled = true;

            if (vt == slCH - 1)
                btnSau.IsEnabled = false;
            else
                btnSau.IsEnabled = true;

            lblCH.Text = strCH[vt];
            rdbA.Content = strA[vt];
            rdbB.Content = strB[vt];
            rdbC.Content = strC[vt];
            rdbD.Content = strD[vt];
            lblSL.Content = "Câu " + (vt + 1) + "/" + slCH;
            reset_RDA();                       
            switch (strChon[vt])
            {
                case "A":                    
                    rdbA.IsChecked = true;
                    rdbA.FontWeight = FontWeights.Bold;
                    break;
                case "B":                   
                    rdbB.IsChecked = true;
                    rdbB.FontWeight = FontWeights.Bold;
                    break;
                case "C":                   
                    rdbC.IsChecked = true;
                    rdbC.FontWeight = FontWeights.Bold;
                    break;
                case "D":                    
                    rdbD.IsChecked = true;
                    rdbD.FontWeight = FontWeights.Bold;
                    break;
                default:
                    
                    break;
            }
            this.UpdateLayout();
        }

        private void hienthi_Thi()
        {
            lblCD.Visibility = Visibility.Hidden;
            stpChonCD.Visibility = Visibility.Hidden;
            dcpThi.Visibility = Visibility.Visible;
            dcpKq.Visibility = Visibility.Hidden;
        }
        private void hienthi_ChuDe()
        {
            lblCD.Visibility = Visibility.Visible;
            stpChonCD.Visibility = Visibility.Visible;
            dcpThi.Visibility = Visibility.Hidden;
            dcpKq.Visibility = Visibility.Hidden;
            txtSoCH.Text = "";
            
        }
        private void hienthi_KQ()
        {
            lblCD.Visibility = Visibility.Hidden;
            stpChonCD.Visibility = Visibility.Hidden;
            dcpThi.Visibility = Visibility.Hidden;
            dcpKq.Visibility = Visibility.Visible;
        }
        private string lay_noidung(string ten, int vt)
        {
            switch (ten)
            {
                case "A":
                    return strA[vt];
                case "B":
                    return strB[vt];
                case "C":
                    return strC[vt];
                case "D":
                    return strD[vt];
                default: return "";
            }

        }
        private void load_DTGKQ()
        {
            dtgKQ.Items.Clear();
            for (int i = 0; i < slCH; i++)
            {
                dtgKQ.Items.Add(new KetQua { stt = i + 1, noidung = strCH[i], chon = lay_noidung(strChon[i], i), dapan = lay_noidung(strDA[i], i) });
            }
            
        }

        private void nopBai()
        {
            int kq = 0;
            for (int i = 0; i < slCH; i++)
            {
                if ((strDA[i] != null) && (strChon[i] != null ))
                {
                    if ((strChon[i].CompareTo(strDA[i]) == 0)) kq++;                
                }                             
            }
            lblTBKQ.Content = "Bạn đã đúng " + kq + "/" + slCH;

            if ((float)kq / slCH > 0.6)
                lblTBKQ.Content += ". Xin chúc mừng!";
            else
                lblTBKQ.Content += ". Bạn cần cố gắng thêm!";

            hienthi_KQ();
            load_DTGKQ();
        }

        private void btnChude_Click(object sender, RoutedEventArgs e)
        {            
            int sl = get_slCH(Convert.ToInt16(txtSoCH.Text), strChude[cboChude.SelectedIndex]);

            if (sl <= 0)
            {
                MessageBox.Show("Chủ đề hiện chưa có câu hỏi!");
                return;
            }

            sql = @"select top " + sl + " * from dbo.cauhoi where cd_ma = '" + strChude[cboChude.SelectedIndex] + "' order by newid()";
            
            DataTable dt = kn.LayDuLieu(sql);
            int rows = dt.Rows.Count;

            if (rows > 0)
            {
                strCH = new string[500];
                strA = new string[500];
                strB = new string[500];
                strC = new string[500];
                strD = new string[500];
                strDA = new string[500];
                strChon = new string[500];
                for (int i = 0; i < rows; i++)
                {
                    strCH[i] = string.Copy(dt.Rows[i][2].ToString());
                    strA[i] = string.Copy(dt.Rows[i][3].ToString());
                    strB[i] = string.Copy(dt.Rows[i][4].ToString());
                    strC[i] = string.Copy(dt.Rows[i][5].ToString());
                    strD[i] = string.Copy(dt.Rows[i][6].ToString());
                    strDA[i] = string.Copy(dt.Rows[i][7].ToString());
                }
                slCH = rows;
            }
            load_CH(pos);
            hienthi_Thi();          
        }

        private void btnTruoc_Click(object sender, RoutedEventArgs e)
        {
            load_CH(--pos);
        }

        private void btnSau_Click(object sender, RoutedEventArgs e)
        {
            load_CH(++pos);
        }

        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            hienthi_ChuDe();
            
        }

        private void rdb_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rdb = sender as RadioButton;

            if (rdb != null)
            {
                reset_RDA();
                switch (rdb.Name)
                {
                    case "rdbA":
                        rdbA.IsChecked = true;
                        rdbA.FontWeight = FontWeights.Bold;
                        strChon[pos] = string.Copy("A");
                        break;
                    case "rdbB":
                        rdbB.IsChecked = true;
                        rdbB.FontWeight = FontWeights.Bold;
                        strChon[pos] = string.Copy("B");
                        break;
                    case "rdbC":
                        rdbC.IsChecked = true;
                        rdbC.FontWeight = FontWeights.Bold;
                        strChon[pos] = string.Copy("C");
                        break;
                    case "rdbD":
                        rdbD.IsChecked = true;
                        rdbD.FontWeight = FontWeights.Bold;
                        strChon[pos] = string.Copy("D"); 
                        break;
                    default: break;
                }
            }
            
        }

        private void btnNop_Click(object sender, RoutedEventArgs e)
        {
            nopBai();
        }

        private void btnThilai_Click(object sender, RoutedEventArgs e)
        {
            hienthi_ChuDe();
            pos = 0;
        }  
              
    }

    public class KetQua
    {
       public int stt{ get; set; }
       public string noidung { get; set; }
       public string chon { get; set; }
       public string dapan { get; set; }
       
       
    }
}
